﻿#ifndef JPIECHART_H
#define JPIECHART_H

#include <QWidget>
#include <QPaintEvent>
#include <QPropertyAnimation>

#define TOTAL_ANGLE         360
#define TOTAL_RADIAN        5760            // 角度转换成弧度时需乘以16
#define PIE_OUT_MARGIN      200             // 饼图外部边距
#define FONTSIZE_FACTOR     15              // 饼图半径是字体大小的倍数

class JPieChart : public QWidget
{
    Q_OBJECT
    Q_PROPERTY(int _radian READ _getValue WRITE _setValue)

public:
    explicit JPieChart(QWidget *parent = nullptr);

private:
    void                initDefaultColorList();
    void                initAnimation();

    void                drawPieWithAnimation(QPainter *painter);
    void                drawPieWithoutAnimation(QPainter *painter);
    void                drawValueTextWithLine(QPainter *painter, QColor color, int radian, int radius, QString text);
    void                drawValueTextWithoutLine(QPainter *painter, QColor color, int radian, int radius, QString text);

    void                _setValue(int radian);
    int                 _getValue();

protected:
    void                paintEvent(QPaintEvent *event);

    // 外部接口
public:
    void                setAnimation(bool useAnimation);
    void                setInnerCircleVisible(bool visible);
    void                setTextLineVisible(bool visible);
    void                setPercentVisible(bool visible);
    void                setValue(QList<double> valueList);

signals:

private:
    QList<QColor>       _defaultColorList1;
    QList<QColor>       _defaultColorList2;

    QList<double>       _valueList;
    QList<double>       _percentList;
    QList<int>          _radianList;

    bool                _useAnimation;
    QPropertyAnimation *_pAnimation;
    int                 _radian;

    bool                _innerCircleVisible;
    bool                _textLineVisible;
    bool                _percentVisible;
};

#endif // JPIECHART_H
